#06/10/2012 Tai-Hsien Ou Yang

FILE_LOCATION   =	"C:\\Users\\Student\\Desktop\\SoapFiles"
FILE_INPUT_PED 	=	"Soapv2.fam"
FILE_OUTPUT	=	"parchild.lst"
FILE_OUTPUT2	=	"gparchild.lst"
FILE_OUTPUT3 =	"siblings.lst"
FILE_OUTPUT4 = "halfsiblings.lst"
FILE_OUTPUT5 =  "background.lst"

setwd(FILE_LOCATION)
dataset.ped<-scan(FILE_INPUT_PED , list(fid=0,id="",pr="",mt="", sex=0, note=0))
  
#	Take out only what we want

pairlist<-c()
pair1<-c()
pair2<-c()

    for(j in 1:(length(dataset.ped$id)))
    {
	
	pair1=cbind(dataset.ped$id[j],dataset.ped$pr[j]) 
	pair2=cbind(dataset.ped$id[j],dataset.ped$mt[j]) 
	pairlist<-rbind(pairlist,pair1)
	pairlist<-rbind(pairlist,pair2)
	
	}
	
write.table(pairlist, FILE_OUTPUT, row.names=F,col.names=F,append=F)



#drewdanohurhurhur
#6/10/2012

poplist<-unique(c(dataset.ped$id,dataset.ped$pr,dataset.ped$mt))
bob<-integer(0)

#for (i in seq(length(poplist)))
#	{
#	if (grepl("-",poplist[i])) bob<-c(bob,-i)
#	else if (!(grepl("K",poplist[i]))) bob<-c(bob,-i)
#	}
#poplist<-poplist[bob]




gpairlist<-c(0,0)
for (i in seq(length(poplist)))
{
temp1<-dataset.ped$pr[which(dataset.ped$id==poplist[i])]
temp2<-dataset.ped$mt[which(dataset.ped$id==poplist[i])]
g1<-dataset.ped$pr[which(dataset.ped$id==temp1)]
g2<-dataset.ped$mt[which(dataset.ped$id==temp1)]
g3<-dataset.ped$pr[which(dataset.ped$id==temp2)]
g4<-dataset.ped$mt[which(dataset.ped$id==temp2)]
gpairlist<-rbind(gpairlist,c(poplist[i],g1))
gpairlist<-rbind(gpairlist,c(poplist[i],g2))
gpairlist<-rbind(gpairlist,c(poplist[i],g3))
gpairlist<-rbind(gpairlist,c(poplist[i],g4))
}

tom<-integer(0)
for (i in seq(length(gpairlist[,1])))
	{
	if (grepl("-",gpairlist[i,1]) | grepl("-",gpairlist[i,2])) tom<-c(tom,-i)
	else if (!(grepl("K",gpairlist[i,1]) & grepl("K",gpairlist[i,2]))) tom<-c(tom,-i)
	}
gpairlist2<-gpairlist[tom,]

tom<-integer(0)
for (i in seq(length(gpairlist2[,1])))
	{
	if (gpairlist2[i,1]==gpairlist2[i,2]) tom<-c(tom,-i)
	}
gpairlist2<-gpairlist2[tom,]

gpairlist<-gpairlist2

write.table(gpairlist, FILE_OUTPUT2, row.names=F,col.names=F,append=F)



############################write the code for sib lists############################
siblist<-c(0,0)
for (i in seq(length(dataset.ped$id)))
{
	temp1<-dataset.ped$pr[i]
	temp2<-dataset.ped$mt[i]
	if (!(grepl("-",temp1)) & (grepl("K",temp1)))
	{
	if (!(grepl("-",temp2)) & (grepl("K",temp2)))
	{
	for (j in (i+1):length(dataset.ped$id))
	{
		if (j<length(dataset.ped$pr)){
		if((dataset.ped$pr[j] == temp1) & (dataset.ped$mt[j] == temp2))
		{siblist<-rbind(siblist,c(dataset.ped$id[j],dataset.ped$id[i]))}}
	}
	}
	}
}

tom<-integer(0)
for (i in seq(length(siblist[,1])))
	{
	if (grepl("-",siblist[i,1]) | grepl("-",siblist[i,2])) tom<-c(tom,-i)
	else if (!(grepl("K",siblist[i,1]) & grepl("K",siblist[i,2]))) tom<-c(tom,-i)
	}
siblist<-siblist[tom,]

write.table(siblist, FILE_OUTPUT3, row.names=F,col.names=F,append=F)





############################write the code for half sib lists################################
halfsiblist<-c(0,0)
for (i in seq(length(dataset.ped$id)))
{
	temp1<-dataset.ped$pr[i]
	temp2<-dataset.ped$mt[i]
	for (j in (i+1):length(dataset.ped$id))
	{
		if (j<length(dataset.ped$pr))
		{
		if((dataset.ped$pr[j] == temp1 & dataset.ped$mt[j] != temp2) | (dataset.ped$pr[j] != temp1 & dataset.ped$mt[j] == temp2))
		{
		if((dataset.ped$pr[j] == temp1 & dataset.ped$mt[j] != temp2) & (!(grepl("-",temp1)) & grepl("K",temp1)))
		{halfsiblist<-rbind(halfsiblist,c(dataset.ped$id[j],dataset.ped$id[i]))}
		if((dataset.ped$pr[j] == temp2 & dataset.ped$mt[j] != temp1) & (!(grepl("-",temp2)) & grepl("K",temp2)))
		{halfsiblist<-rbind(halfsiblist,c(dataset.ped$id[j],dataset.ped$id[i]))}
		}
		}
	}
}

tom<-integer(0)
for (i in seq(length(halfsiblist[,1])))
	{
	if (grepl("-",halfsiblist[i,1]) | grepl("-",halfsiblist[i,2])) tom<-c(tom,-i)
	else if (!(grepl("K",halfsiblist[i,1]) & grepl("K",halfsiblist[i,2]))) tom<-c(tom,-i)
	}
halfsiblist<-halfsiblist[tom,]

write.table(halfsiblist, FILE_OUTPUT4, row.names=F,col.names=F,append=F)
################################################################################################################


#make some matrices for storing total length, and number of segments

Lengths <- matrix(0,length(poplist),length(poplist))
Numbers <-Lengths

con <- file('Soapv2.1.match', 'r')
while (length(input <- readLines(con, n=1000)) > 0)
{
    for (i in 1:length(input))
	{
		temp<-unlist(strsplit(unlist(strsplit(input[i],'\t'))," "))[c(2,4,11)]
		if ((A<-which(poplist==temp[1]))<(B<-which(poplist==temp[2])))
		{Lengths[A,B] = Lengths[A,B] + as.integer(temp[3])
		 Numbers[A,B] = Numbers[A,B] +1}
		else {Lengths[B,A] = Lengths[B,A] + as.integer(temp[3])
			Numbers[B,A] = Numbers[B,A] +1}
	}
}


write.table(Lengths,"Lengths.txt",row.names=F,col.names=F,quote=F)
write.table(Numbers,"Numbers.txt",row.names=F,col.names=F,quote=F)



con <- file('Soapv2.2.match', 'r')
while (length(input <- readLines(con, n=1000)) > 0)
{
    for (i in 1:length(input))
	{
		temp<-unlist(strsplit(unlist(strsplit(input[i],'\t'))," "))[c(2,4,11)]
		if ((A<-which(poplist==temp[1]))<(B<-which(poplist==temp[2])))
		{Lengths[A,B] = Lengths[A,B] + as.integer(temp[3])
		 Numbers[A,B] = Numbers[A,B] +1}
		else {Lengths[B,A] = Lengths[B,A] + as.integer(temp[3])
			Numbers[B,A] = Numbers[B,A] +1}
	}
}


#write.table(Lengths,"Lengths.txt",row.names=F,col.names=F,quote=F)
#write.table(Numbers,"Numbers.txt",row.names=F,col.names=F,quote=F)

for (p in 3:22)
{
con <- file(paste('Soapv2.',p,'.match',sep=''), 'r')
while (length(input <- readLines(con, n=1000)) > 0)
{
    for (i in 1:length(input))
	{
		temp<-unlist(strsplit(unlist(strsplit(input[i],'\t'))," "))[c(2,4,11)]
		if ((A<-which(poplist==temp[1]))<(B<-which(poplist==temp[2])))
		{Lengths[A,B] = Lengths[A,B] + as.integer(temp[3])
		 Numbers[A,B] = Numbers[A,B] +1}
		else {Lengths[B,A] = Lengths[B,A] + as.integer(temp[3])
			Numbers[B,A] = Numbers[B,A] +1}
	}
}
}

#need to loop for ALL! and then write.. this takes a LONG TIME
#write.table(Lengths,"Lengths.txt",row.names=F,col.names=F,quote=F)
#write.table(Numbers,"Numbers.txt",row.names=F,col.names=F,quote=F)





itsiklist<-c(0,0," ")
for  (i in 1:length(gpairlist[,1]))
{
	itsiklist<-rbind(itsiklist,c(gpairlist[i,1],gpairlist[i,2],"GC"))
}
for  (i in 1:length(pairlist[,1]))
{
	itsiklist<-rbind(itsiklist,c(pairlist[i,1],pairlist[i,2],"PC"))
}
for  (i in 1:length(siblist[,1]))
{
	itsiklist<-rbind(itsiklist,c(siblist[i,1],siblist[i,2],"SB"))
}
for  (i in 1:length(halfsiblist[,1]))
{
	itsiklist<-rbind(itsiklist,c(halfsiblist[i,1],halfsiblist[i,2],"HB"))
}

write.table(itsiklist,"itsiklist.txt",row.names=F,col.names=F,quote=F)

